<?php
//note to self: microdegrees is E6
function returnDistance($lat1, $lon1, $lat2, $lon2, $distance) { 
	//convert to degrees
	$lat1 = (float)$lat1/1000000;
	$lon1 = (float)$lon1/1000000;
	$lat2 = (float)$lat2/1000000;
	$lon2 = (float)$lon2/1000000;
	$epsilon = (int)$lon1 - (int)$lon2;
	$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($epsilon)); 
	$dist = rad2deg(acos($dist)); 
	$miles = $dist * 60 * 1.1515;
	if($miles <= $distance){
		return 1;
	}
	else{
		return 0;
		}
	}
	$longitude1 = $_POST['longitude'];
	$latitude1 = $_POST['latitude'];
	$radius = $_POST['radius'];
	$connectstatus = mysql_connect("localhost", "root", "");
if (!$connectstatus){
	die('Couldn\'t connect: ' . mysql_error ());
	}
	mysql_select_db("captsone-tagit",$connectstatus);
	$query = mysql_query("SELECT * from test_table");
	while($row = mysql_fetch_array($query)){
		if(returnDistance($latitude1, $longitude1, $row['Latitude'], $row['Longitude'], $radius) == 1){
			echo $row['Type'] . "|" . $row['Title'] . "|" .$row['Description']. "|". $row['Latitude'] . "|" . $row['Longitude'] . "|" . $row['Date'] . "|" . $row['Attributes'];
			echo "\n";
		}

	}
mysql_close($connectstatus);
?>
